<?php

//Product
     if(!$txtProduct) {
         array_push($ActionResults, "Please enter Product Name.");
         $intFailed++;
     }else{
	 	// replace ' with &#039;
		$txtProduct = str_replace("'","&#039;",$txtProduct);
	 }

      if(!$txtDescription) {
         array_push($ActionResults, "Please enter Description.");
         $intFailed++;
     }


     if(!$cboSupplier) {
         array_push($ActionResults, "Please enter Supplier.");
         $intFailed++;
     }


     if(!$cboCategory) {
         array_push($ActionResults, "Please enter Category.");
         $intFailed++;
     }

//QuantityPerUnit
     if(!$txtQuantityPerUnit) {
         array_push($ActionResults, "Please enter Quantity Per Unit.");
         $intFailed++;
     }

//UnitPrice
     if(!$txtUnitPrice) {
         array_push($ActionResults, "Please enter Unit Retail Price (Including Vat).");
         $intFailed++;
     }else{
	 	$txtUnitPrice = str_replace(",","",$txtUnitPrice);
	 }
	 
//FullPrice
     if(!$txtFullPrice) {
         array_push($ActionResults, "Please enter Recommended Retail Price (Including Vat).");
         $intFailed++;
     }else{
	 	$txtFullPrice = str_replace(",","",$txtFullPrice);
	 }
	 
     if($cboUploadedPicture == 1) {
     
	 	if (!is_uploaded_file($txtUploadedPicture)){
		//if (!$txtPicture){
			array_push($ActionResults,"Your Photo Not Attached, Please Try again");
			$intFailed++;
		} 
	 }
	 
	 
//MetaTitle
     if(!$txtMetaTitle) {
         array_push($ActionResults, "Please enter Meta Title.");
         $intFailed++;
     }else{
         //$txtMetaTitle = ucwords(strtolower($txtMetaTitle)); 
     }

//MetaDescription
     if(!$txtMetaDescription) {
         array_push($ActionResults, "Please enter Meta Description.");
         $intFailed++;
     }else{
        // $txtMetaDescription = ucwords(strtolower($txtMetaDescription)); 
     }

      if(!$txtMetaKeyword) {
         array_push($ActionResults, "Please enter Meta Keyword.");
         $intFailed++;
     }else{
         //$txtMetaKeyword = ucfirst(strtolower($txtMetaKeyword)); 
     }
	 
     if($intFailed > 0){
         return(FALSE);
     }

     if ($intProductID){
        $strSQL = "Update tblProduct
                     SET   tblProduct.Product =  '$txtProduct'
                         , tblProduct.Description =  '$txtDescription'
                         , tblProduct.ExtraDescription =  '$txtExtraDescription'
                         , tblProduct.Weight =  '$cboWeight'
                         , tblProduct.OwnProductCode =  '$txtOwnProductCode'
                         , tblProduct.Barcode =  '$txtBarcode'
                         , tblProduct.Supplier =  '$cboSupplier'
                         , tblProduct.SupplierProductID =  '$txtSupplierProductID'
                         , tblProduct.Category =  '$cboCategory'
                         , tblProduct.QuantityPerUnit =  '$txtQuantityPerUnit'
						 , tblProduct.CostPrice =  '$txtCostPrice'
                         , tblProduct.UnitPrice =  '$txtUnitPrice'
						 , tblProduct.FullPrice =  '$txtFullPrice'
                         , tblProduct.VatRate =  '$cboVatRate'
                         , tblProduct.UnitsInStock =  '$txtUnitsInStock'
                         , tblProduct.UnitsOnOrder =  '$txtUnitsOnOrder'
                         , tblProduct.ReorderLevel =  '$txtReorderLevel'
                         , tblProduct.Discontinued =  '$chkDiscontinued'
                         , tblProduct.PromotionalItem =  '$chkPromotionalItem'
						 , tblProduct.MetaTitle =  '$txtMetaTitle'
                         , tblProduct.MetaDescription =  '$txtMetaDescription'
                         , tblProduct.MetaKeyword =  '$txtMetaKeyword'
                         , tblProduct.MetaComment =  '$txtMetaComment' 
						 , tblProduct.DisplayOrder =  '$txtDisplayOrder'
						 , tblProduct.DateUpdated = NOW()	
						 , tblProduct.UpdatedBy = '".$_SESSION['sesStaffID']."'
     "; 
        $strJoin = " WHERE ";
        $strSQL .= $strJoin."tblProduct.ID =  '$intProductID'";
        $strMessage = " Updated";
    }Else{

        $strSQL = "INSERT INTO tblProduct
                     (     tblProduct.Product
                         , tblProduct.Description
                         , tblProduct.ExtraDescription
                         , tblProduct.Weight
                         , tblProduct.OwnProductCode
                         , tblProduct.Barcode
                         , tblProduct.Supplier
                         , tblProduct.SupplierProductID
                         , tblProduct.Category
                         , tblProduct.QuantityPerUnit
						 , tblProduct.CostPrice
                         , tblProduct.UnitPrice
						 , tblProduct.FullPrice
                         , tblProduct.VatRate
                         , tblProduct.UnitsInStock
                         , tblProduct.UnitsOnOrder
                         , tblProduct.ReorderLevel
                         , tblProduct.Discontinued
                         , tblProduct.UploadedPicture
                         , tblProduct.PromotionalItem
						 , tblProduct.MetaTitle
                         , tblProduct.MetaDescription
                         , tblProduct.MetaKeyword
                         , tblProduct.MetaComment
						 , tblProduct.DisplayOrder
						 , tblProduct.DateUpdated
						 , tblProduct.UpdatedBy
                     )Values( '$txtProduct'
                         , '$txtDescription'
                         , '$txtExtraDescription'
                         , '$cboWeight'
                         , '$txtOwnProductCode'
                         , '$txtBarcode'
                         , '$cboSupplier'
                         , '$txtSupplierProductID'
                         , '$cboCategory'
                         , '$txtQuantityPerUnit'
						 , '$txtCostPrice'
                         , '$txtUnitPrice'
						 , '$txtFullPrice'
                         , '$cboVatRate'
                         , '$txtUnitsInStock'
                         , '$txtUnitsOnOrder'
                         , '$txtReorderLevel'
                         , '$chkDiscontinued'
						 , '".strtoupper(trim($txtSupplierProductID)).".jpg'
                         , '$chkPromotionalItem'
						 , '$txtMetaTitle'
                         , '$txtMetaDescription'
                         , '$txtMetaKeyword'
                         , '$txtMetaComment' 
						 , '$txtDisplayOrder'
						 , NOW()	
						 , '".$_SESSION['sesStaffID']."'					 
                       )";
        $strMessage = "added";
        echo "<!-- $strSQL -->";
    }
    $strSQL .= ";";
	//echo $strSQL;
         if (!include (MODULE . "/action/actMySqlResults")){
             array_push($ActionResults,"Failed To Add Product To Database");
             return(false);
         }
     if (!$intProductID){
         $intProductID = mysql_insert_id();
     }

	 //echo "cbo ".$cboUploadedPicture;
     if($cboUploadedPicture == 1) {
     
	 	if (!is_uploaded_file($txtUploadedPicture)){
			array_push($ActionResults,"Your Photo Not Attached, Please Try again");
			return(FALSE);
		}else{
			array_push($ActionResults,"DEBUG Photo Attached Ok");
		} 
		
		//(substr($txtPicture_type,0,11)=="image/x-png") xor(substr($txtPicture_type,0,10)=="image/gif")
			
		if (!(substr($txtUploadedPicture_type,0,11)=="image/pjpeg") xor 
			(substr($txtUploadedPicture_type,0,10)=="image/jpeg")){
			array_push($ActionResults,"Picture Upload Failed, Please Supply Picture As JPEG format");
			return(FALSE);
		}else{
			//array_push($ActionResults,"DEBUG Photo is jpg");
		}
		
         if(!copy($txtUploadedPicture,"./images/product/product".$intProductID.".jpg")){
             array_push($ActionResults,"Failed To Copy Picture from tmp to ./images/product/product".$intProductID.".jpg");
             return(FALSE);
         }else{
			//array_push($ActionResults,"DEBUG Photo copied");
		 }
		 
         if(!copy($txtUploadedPicture,"./images/product/thumbz/product".$intProductID.".jpg")){
             array_push($ActionResults,"Failed To Copy picture To thumbnail library");
             return(FALSE);
         }else{
			//array_push($ActionResults,"DEBUG Copied to thumbnail directory");
		 }
		 
			 if(PRODUCT_LARGE_PICTURE_WIDTH > 0){
			 
				if(!copy($txtUploadedPicture,"./images/product/largeproduct".$intProductID.".jpg")){
		             array_push($ActionResults,"Failed To Copy ".PRODUCT_LARGE_PICTURE_WIDTH." pixel picture To thumbnail library");
		             return(FALSE);
		         }
				 
				 if(CreateThumbNail("./images/product/","largeproduct".$intProductID.".jpg",PRODUCT_LARGE_PICTURE_WIDTH)){
					array_push($ActionResults,"Resize of ".PRODUCT_LARGE_PICTURE_WIDTH." pixel picture failed");
					return(FALSE);
				 }
			 }
			 
		 // Shrink copied picture
     	if(CreateThumbNail("./images/product/","product".$intProductID.".jpg",PRODUCT_PICTURE_WIDTH)){
			array_push($ActionResults,"Resize of ".PRODUCT_PICTURE_WIDTH." pixel picture failed");
			return(FALSE);
		}else{
			//array_push($ActionResults,"DEBUG Photo resized to ".PRODUCT_PICTURE_WIDTH."px created");
		}
		
     	if(CreateThumbNail("./images/product/","product".$intProductID.".jpg",PRODUCT_THUMBNAIL_WIDTH,"thumbz/")){
			array_push($ActionResults,"Thumbnail Creation Failed");
			return(FALSE);
		}else{
			//array_push($ActionResults,"DEBUG Photo thumbnail ".PRODUCT_PICTURE_WIDTH."px created");
		}
			
	     $strSQL = "Update tblProduct
	                 SET tblProduct.UploadedPicture = 'product".$intProductID.".jpg'
	                 WHERE
	                 tblProduct.ID = ".$intProductID;
	     include (MODULE . "/action/actMySqlResults");
	     unlink($txtUploadedPicture);
		 unset($intProductID);
     }else{
	 	//no picture to process
		unset($intProductID);
		unset($_REQUEST['intProductID']);
	 }
	 
	 //Recreate menu table. Menu written to table to improve performance on large sites.
	include (MODULE . "/action/actRecreateMenu");
?>
